package queries;

import controller.LollaWrapper;
import entities.Band;
import entities.Bands;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class BandQueries extends AbstractQueries {
    private long pageCount;
    private final String baseBandUrl;
    private final LollaWrapper wrap;

    public BandQueries(LollaWrapper wrap) throws IOException {
        this.wrap = wrap;
        baseBandUrl = wrap.createFullAPIUrl("bands");
        Bands bands = wrap.makeCall(baseBandUrl, Bands.class);
        pageCount = bands.getTotalPages();
    }

    private List<Band> getBandsByPage(int page) throws IOException {
        String url = baseBandUrl + "&page=" + page;
        return wrap.makeCall(url, Bands.class).getBands();
    }

    public List<Band> getAllBands() throws IOException {
        List<Band> allBands = new ArrayList<Band>();
        for (int i = 1; i <= pageCount; i++) {
            allBands.addAll(getBandsByPage(i));
        }
        return allBands;
    }

    public Band getBandById(long id) throws IOException {
        return wrap.makeCall(wrap.createFullAPIUrl("bands/" + id), Band.class);
    }

    public long getBandFanCountById(long id) throws IOException {
        return wrap.makeCall(wrap.createFullAPIUrl("bands/" + id + "/current_votes"), Band.class).getFanCount();
    }
}
